package com.quocnv.mediascanner;

import java.io.File;
import java.util.ArrayList;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SmbFileAdapter extends BaseAdapter
{
	public final int TYPE_DIRECTORY = 0;
	public final int TYPE_VIDEO = 1;
	public final int TYPE_AUDIO = 2;

	private ArrayList<SmbFile> mSmbFiles;
	private Context context;
	private LayoutInflater mInflater;

	public SmbFileAdapter(Context ctx, ArrayList<SmbFile> smbFiles)
	{
		this.context = ctx;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mSmbFiles = smbFiles;
	}

	@Override
	public int getItemViewType(int position)
	{
		SmbFile file = mSmbFiles.get(position);

		try
		{
			if (file == null || file.isDirectory())
				return TYPE_DIRECTORY;
			else
			{
				if (SmbFileFilterUtil.getAudioFilter().accept(file))
					return TYPE_AUDIO;
				else if (SmbFileFilterUtil.getVideoFilter().accept(file))
					return TYPE_VIDEO;
			}

		} catch (SmbException e)
		{
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int getCount()
	{
		return mSmbFiles.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mSmbFiles.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;

		int type = getItemViewType(position);
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.file_row, parent, false);
			holder = new ViewHolder();
			holder.mImg = (ImageView) convertView.findViewById(R.id.ivIcon);
			holder.mName = (TextView) convertView.findViewById(R.id.tvName);
			holder.mSize = (TextView) convertView.findViewById(R.id.tvSize);

			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}

		SmbFile file = mSmbFiles.get(position);
		try
		{
			if (file == null || (position == 0 && file.isDirectory()))
				holder.mName.setText(".."); // Item back
			else if (file != null)
				holder.mName.setText(file.getName());
		} catch (SmbException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		switch (type)
		{
		case TYPE_DIRECTORY:
			holder.mImg.setImageDrawable(context.getResources().getDrawable(
					R.drawable.folder_icon));
			holder.mSize.setText("[DIR]");
			break;
		case TYPE_VIDEO:
			holder.mImg.setImageDrawable(context.getResources().getDrawable(
					R.drawable.video_icon));
			try
			{
				holder.mSize.setText(String.valueOf(file.length()));
			} catch (SmbException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case TYPE_AUDIO:
			holder.mImg.setImageDrawable(context.getResources().getDrawable(
					R.drawable.audio_icon));
			try
			{
				holder.mSize.setText(String.valueOf(file.length()
						/ (1024 * 1024))
						+ "MB");
			} catch (SmbException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}

		return convertView;
	}

	public void clear()
	{
		mSmbFiles = new ArrayList<SmbFile>();
	}

	public ArrayList<SmbFile> getSmbFiles()
	{
		return mSmbFiles;
	}

	public void setSmbFiles(ArrayList<SmbFile> mSmbFiles)
	{
		this.mSmbFiles = mSmbFiles;
	}

	private static class ViewHolder
	{
		TextView mName;
		TextView mSize;
		ImageView mImg;
	}
}
